package com.youyi.yyfilepickerlibrary.core.loader;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import java.lang.ref.WeakReference;

public class EssAlbumCollection implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final int LOADER_ID = 1;
    private EssAlbumCallbacks mCallbacks;
    private WeakReference<Context> mContext;
    private LoaderManager mLoaderManager;

    public interface EssAlbumCallbacks {
        void onAlbumMediaLoad(Cursor cursor);

        void onAlbumMediaReset();
    }

    @Override // android.support.v4.app.LoaderManager.LoaderCallbacks
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        Context context = this.mContext.get();
        if (context == null) {
            return null;
        }
        return EssAlbumLoader.newInstance(context);
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (this.mContext.get() != null) {
            this.mCallbacks.onAlbumMediaLoad(cursor);
        }
    }

    @Override // android.support.v4.app.LoaderManager.LoaderCallbacks
    public void onLoaderReset(Loader<Cursor> loader) {
        if (this.mContext.get() != null) {
            this.mCallbacks.onAlbumMediaReset();
        }
    }

    public void onCreate(FragmentActivity fragmentActivity, EssAlbumCallbacks essAlbumCallbacks) {
        this.mContext = new WeakReference<>(fragmentActivity);
        this.mLoaderManager = fragmentActivity.getSupportLoaderManager();
        this.mCallbacks = essAlbumCallbacks;
    }

    public void onDestroy() {
        this.mLoaderManager.destroyLoader(1);
        this.mCallbacks = null;
    }

    public void load() {
        this.mLoaderManager.initLoader(1, new Bundle(), this);
    }
}
